﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Models;
using DAL;
using WebShopAPI.Models;
using System.Web.Script.Serialization;
namespace WebShopAPI.Controllers
{
    public class xiaddController : ApiController
    {
        sp_orderServcie nn = new sp_orderServcie();
        sp_ProductServcie nnn = new sp_ProductServcie();
        sp_order_goodsDAL n = new sp_order_goodsDAL();
        sp_user_cartDAL nnnnn = new sp_user_cartDAL();
        public ResponData post(jsdx dx)
        {
            int _sn = 0;
            sp_Product dxx = nnn.cx().Where(p => p.Good_SKU_Attr == dx.spname && p.Goods_id == dx.spid).FirstOrDefault();
            sp_order s = new sp_order();
            s.user_id = dx.yhid;
            s.order_number = DateTime.Now.ToString("yyyyMMddHHmmss") +nn.GetAll().Count();
            s.order_price = (decimal)dxx.Good_SKU_KCPrice * dx.sl;
            s.order_pay = 0;
            s.is_send = "否";
            s.trade_no = "";
            s.order_fapiao_title = "个人";
            s.order_fapiao_company = "";
            s.order_fapiao_content = "";
            s.consignee_addr = dx.dzid;
            s.pay_status = 0;
            s.create_time = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmss"));
            s.update_time = 1512535620;
            int x = nn.Add(s);
            if (x > 0)
            {
                   
                    sp_order_goods ss = new sp_order_goods();
                    ss.order_id = s.order_id;
                    ss.goods_id = dxx.Id;
                    ss.goods_price = (decimal)dxx.Good_Price;
                    ss.goods_number = dx.sl;
                    ss.goods_total_price = (decimal)dxx.Good_Price * ss.goods_number;
                    n.add(ss);
            }
            ResponData d = new ResponData();
            d.data = s;
            d.meta.msg = "添加成功";
            d.meta.status = 200;
            return d;
        }
        [Route("api/xiadd/dtsj")]
        public ResponData post(js dx)
        {
            sp_user_cart sp_Users = nnnnn.cx().Where(f => f.user_id == dx.yhid).FirstOrDefault();
            List<gwsp> gg = new List<gwsp>();
            JavaScriptSerializer j = new JavaScriptSerializer();
            gg = j.Deserialize<List<gwsp>>(sp_Users.cart_info);
            for (int i = 0; i < dx.spsj.Count; i++)
            {
                for (int jj = 0; jj < gg.Count; jj++)
                {
                    if (gg[jj].goods_id == dx.spsj[i].Id)
                    {
                        gg.Remove(gg[jj]);
                    }
                }
            }
            string sql = j.Serialize(gg);
            sp_user_cart uu = new sp_user_cart();
            uu.cart_id = sp_Users.cart_id;
            uu.user_id = dx.yhid;
            uu.cart_info = sql;
            uu.created_at = null;
            uu.updated_at = null;
            uu.delete_time = null;
            int hh = nnnnn.xg(uu);

            int _sn = 0;
            sp_order s = new sp_order();
            s.user_id = dx.yhid;
            s.order_number = DateTime.Now.ToString("yyyyMMddHHmmss") + nn.GetAll().Count();
            s.order_price = (decimal)dx.zjg;
            s.order_pay = 0;
            s.is_send = "否";
            s.trade_no = "";
            s.order_fapiao_title = "个人";
            s.order_fapiao_company = "";
            s.order_fapiao_content = "";
            s.consignee_addr = dx.dzid;
            s.pay_status = 0;
            s.create_time = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmss"));
            s.update_time = 1512535620;
            int x = nn.Add(s);
            if (x > 0)
            {
                for (int i = 0; i < dx.spsj.Count; i++)
                {
                    sp_order_goods ss = new sp_order_goods();
                    ss.order_id = s.order_id;
                    ss.goods_id = dx.spsj[i].Id;
                    ss.goods_price = (decimal)dx.spsj[i].Good_Price;
                    ss.goods_number = dx.slsj[i];
                    ss.goods_total_price = (decimal)dx.spsj[i].Good_Price * ss.goods_number;
                    n.add(ss);
                }
            }
            ResponData d = new ResponData();
            d.data = s;
            d.meta.msg = "添加成功";
            d.meta.status = 200;
            return d;
        }
        public ResponData get(int id)
        {
            sp_order dx= nn.GetOne(id);
            dx.order_pay = 1;
            ResponData d = new ResponData();
            d.data = nn.Modify(dx);
            d.meta.msg = "支付成功";
            d.meta.status = 200;
            return d;
        }
    }
    public class jsdx{
        public int yhid { get; set; }
        public string dzid { get; set; }
        public int sl { get; set; }
        public int spid { get; set; }
        public string spname { get; set; }
    }
    public class js
    {
        public int yhid { get; set; }
        public List<sp_Product> spsj { get; set; }
        public List<int> slsj { get; set; }
        public string dzid { get; set; }
        public double zjg { get; set; }
    }
}
